<template>
  <div
    class="demo-block"
    :class="[blockClass, { 'hover': hovering }]"
    @mouseenter="hovering = true"
    @mouseleave="hovering = false">
    <div class="source">
      <slot name="source"></slot>
    </div>
    <div class="meta" ref="meta">
      <div class="description" v-if="$slots.default">
        <slot></slot>
      </div>
      <div class="highlight">
        <slot name="highlight" :code="code"></slot>
      </div>
      <div class="hidden-code">
        <slot name="hiddenCode"></slot>
      </div>
    </div>
    <div
      class="demo-block-control"
      ref="control"
      :class="{ 'is-fixed': fixedControl }"
      @click="isExpanded = !isExpanded">
      <transition name="arrow-slide">
        <i :class="[iconClass, { 'hovering': hovering }]"></i>
      </transition>
      <transition name="text-slide">
        <span v-show="hovering">{{ controlText }}</span>
      </transition>
      <transition name="text-slide">
        <button
          type="text"
          class="control-button"
          @click.stop="goCodeRun">
          在线运行
        </button>
      </transition>
    </div>
  </div>
</template>

<script>
  import hlit from 'highlight.js'
  import 'highlight.js/styles/github.css';
  export default {
    data() {
      return {
        hovering: false,
        isExpanded: false,
        fixedControl: false,
        scrollParent: null,
        code: '',
      };
    },
    methods: {
      scrollHandler() {
        if (this.$refs && this.$refs.meta) {
          const { top, bottom, left } = this.$refs.meta.getBoundingClientRect();
          this.fixedControl = bottom > document.documentElement.clientHeight &&
            top + 44 <= document.documentElement.clientHeight;
          this.$refs.control.style.left = this.fixedControl ? `${ left }px` : '0';
        }
      },
      removeScrollHandler() {
        this.scrollParent && this.scrollParent.removeEventListener('scroll', this.scrollHandler);
      },
      goCodeRun() {
        console.log(this);
        console.log('run');
      },
    },
    computed: {
      blockClass() {
        return `demo-ZH-CN demo-${ this.$router.currentRoute.path.split('/').pop() }`;
      },
      iconClass() {
        return this.isExpanded ? 'vuepress-caret-top' : 'vuepress-caret-bottom';
      },
      controlText() {
        return this.isExpanded ? '隐藏代码' : '显示代码';
      },
      codeArea() {
        return this.$el.getElementsByClassName('meta')[0];
      },
      codeAreaHeight() {
        if (this.$el.getElementsByClassName('description').length > 0) {
          return this.$el.getElementsByClassName('description')[0].clientHeight +
            this.$el.getElementsByClassName('highlight')[0].clientHeight + 20;
        }
        return this.$el.getElementsByClassName('highlight')[0].clientHeight;
      }
    },
    watch: {
      isExpanded(val) {
        this.codeArea.style.height = val ? `${ this.codeAreaHeight + 1 }px` : '0';
        if (!val) {
          this.fixedControl = false;
          this.$refs.control.style.left = '0';
          this.removeScrollHandler();
          return;
        }
        this.$nextTick(() => {
          setTimeout(() => {
            this.scrollParent = document.querySelector('.page');
            if (this.scrollParent) {
              window.addEventListener('scroll', this.scrollHandler);
            }
            this.scrollHandler();
            console.log(this.code);
          }, 200);
        })
      }
    },
    created() {
      const highlight = this.$slots.highlight;
      if (highlight && highlight[0]) {
        let code = '';
        let cur = highlight[0];
        if (cur.tag === 'pre' && (cur.children && cur.children[0])) {
          cur = cur.children[0];
          if (cur.tag === 'code') {
            code = cur.children[0].text;
          }
        }
      }
      console.log(highlight);
    },
    mounted() {
      this.$nextTick(() => {
        let highlight = this.$el.getElementsByClassName('highlight')[0];
        let blocks = this.$el.querySelectorAll('pre code');
        blocks.forEach((block) => {
          hlit.highlightBlock(block)
        });
        if (this.$el.getElementsByClassName('description').length === 0) {
          highlight.style.width = '100%';
          highlight.borderRight = 'none';
        }
      });
    },
    beforeDestroy() {
      this.removeScrollHandler();
    }
  };
</script>

<style lang="less">
  .demo-block {
    border: solid 1px #ebebeb;
    border-radius: 3px;
    transition: .2s;
    margin: 30px 0 20px 0;
    &.hover {
      box-shadow: 0 0 8px 0 rgba(232, 237, 250, .6), 0 2px 4px 0 rgba(232, 237, 250, .5);
    }
    code {
      font-family: Menlo, Monaco, Consolas, Courier, monospace;
    }
    .demo-button {
      float: right;
    }
    .source {
      padding: 0;
      .source-content {
        width: 100%;
        height: 500px;
      }
    }
    .meta {
      background-color: #fafafa;
      border-top: solid 1px #eaeefb;
      overflow: hidden;
      height: 0;
      transition: height .2s;
    }
    .description {
      padding: 20px;
      box-sizing: border-box;
      border: solid 1px #ebebeb;
      border-radius: 3px;
      font-size: 14px;
      line-height: 22px;
      color: #666;
      word-break: break-word;
      margin: 10px;
      background-color: #fff;
      p {
        margin: 0;
        line-height: 26px;
      }
      code {
        color: #5e6d82;
        background-color: #e6effb;
        margin: 0 4px;
        display: inline-block;
        padding: 1px 5px;
        font-size: 12px;
        border-radius: 3px;
        height: 18px;
        line-height: 18px;
      }
    }
    .highlight {
      pre {
        margin: 0;
      }
      code.hljs {
        margin: 0;
        border: none;
        max-height: none;
        border-radius: 0;
        &::before {
          content: none;
        }
      }
    }
    .demo-block-control {
      border-top: solid 1px #eaeefb;
      height: 44px;
      box-sizing: border-box;
      background-color: #fff;
      border-bottom-left-radius: 4px;
      border-bottom-right-radius: 4px;
      text-align: center;
      margin-top: -1px;
      color: #d3dce6;
      cursor: pointer;
      position: relative;

      &.is-fixed {
        position: fixed;
        bottom: 0;
        width: calc(100% - 38px);
        max-width: 740px;
        z-index: 999;
      }
      .vuepress-caret-top {
        &::before {
          content: '';
          display: inline-block;
          border-left: 6px solid transparent;
          border-right: 6px solid transparent;
          border-bottom: 8px solid #d3dce6;
          transform: translate(-3, 0);
        }

        &.hovering::before {
          transition: .3s;
          transform: translateX(-45px);
        }
      }

      .vuepress-caret-bottom {
        &::before {
          content: '';
          display: inline-block;
          border-left: 6px solid transparent;
          border-right: 6px solid transparent;
          border-top: 8px solid #d3dce6;
          transform: translate(-3, 0);
        }
        &.hovering::before {
          transition: .3s;
          transform: translateX(-45px);
        }
      }
      i {
        font-size: 16px;
        line-height: 44px;
        transition: .3s;
      }
      > span {
        position: absolute;
        transform: translateX(-30px);
        font-size: 14px;
        line-height: 44px;
        transition: .3s;
        display: inline-block;
      }
      &:hover {
        color: #349EF7;
        background-color: #f9fafc;
      }
      & .text-slide-enter,
      & .text-slide-leave-active {
        opacity: 0;
        transform: translateX(10px);
      }

      .control-button {
        line-height: 26px;
        position: absolute;
        top: 0;
        right: 0;
        font-size: 14px;
        padding: 9px 25px 9px 5px;
        border-radius: 3px;
        border-color: transparent;
        color: #349EF7;
        background: transparent;
        display: inline-block;
        white-space: nowrap;
        cursor: pointer;
        text-align: center;
        box-sizing: border-box;
        outline: none;
        margin: 0;
        transition: .1s;
        font-weight: 500;
      }
    }
  }
</style>
